#include "Audio.h"

Sample::Sample()
{
	sample = NULL;
	channel = NULL;
}

Sample::~Sample()
{
	if( sample != NULL )
	{
		FMOD_Sound_Release( sample );
		sample = NULL;
	}
}

Audio::Audio()
{
	system = NULL;
}

Audio::~Audio()
{
	//release the samples
	for( Iterator i = samples.begin(); i != samples.end(); i++ )
	{
		(*i) = NULL;
	}
	FMOD_System_Release(system);
}

bool Audio::Init()
{
	if( FMOD_System_Create(&system) != FMOD_OK )
	{
		return false;
	}

	if( FMOD_System_Init( system, 100, FMOD_INIT_NORMAL, NULL ) != FMOD_OK )
	{
		return false;
	}
	return true;
}

void Audio::Update()
{
	FMOD_System_Update( system );
}

Sample* Audio::Load( std::string filename )
{
	if( filename.length() == 0 ) return false;

	Sample* sample = new Sample();
	FMOD_RESULT res;
	res = FMOD_System_CreateSound( 
		system,
		filename.c_str(),	//filename
		FMOD_DEFAULT,		//default audio
		NULL,
		&sample->sample);	//pointer to sample
	if( res != FMOD_OK )
	{
		sample = NULL;
	}
	return sample;
}

bool Audio::SampleExists( std::string name )
{
	for( Iterator i = samples.begin(); i != samples.end(); i++ )
	{
		if( (*i)->getName() == name )
		{
			return true;
		}
	}
	return false;
}

bool Audio::IsPlaying( std::string name )
{
	Sample * samp = FindSample(name);
	if( samp == NULL ) return false;

	int index;
	FMOD_Channel_GetIndex( samp->channel, &index ); //99 if playing, 0 if not
	return ( index > 0 );
}

Sample* Audio::FindSample( std::string name )
{
	Sample* sample = NULL;
	for( Iterator i = samples.begin(); i != samples.end(); i++ )
	{
		if( (*i)->getName() == name )
		{
			sample = (*i);
			break;
		}
	}
	return sample;
}

bool Audio::Play( std::string name )
{
	FMOD_RESULT res;
	Sample * sample = FindSample(name);
	if( sample->sample != NULL )
	{
		//sample found, play it
		res = FMOD_System_PlaySound(
			system,
			FMOD_CHANNEL_FREE,
			sample->sample,
			true,
			&sample->channel);

		if( res != FMOD_OK ) return false;
		FMOD_Channel_SetLoopCount( sample->channel , -1 );
		FMOD_Channel_SetPaused( sample->channel, false );
	}
	return true;
}

bool Audio::Play( Sample* sample )
{
	FMOD_RESULT res;
	if( sample == NULL ) return false;
	if( sample->sample == NULL ) return false;
	res = FMOD_System_PlaySound(
		system,
		FMOD_CHANNEL_FREE,
		sample->sample,
		true,
		&sample->channel);

	if( res != FMOD_OK ) return false;
	FMOD_Channel_SetLoopCount( sample->channel , -1 );
	FMOD_Channel_SetPaused( sample->channel , false );
	return true;
}

void Audio::Stop( std::string name )
{
	if( !IsPlaying(name) ) return;
	Sample* sample = FindSample( name );
	if( sample == NULL ) return;
	FMOD_Channel_Stop( sample->channel );
}

void Audio::StopAll()
{
	for( Iterator i = samples.begin(); i != samples.end(); i++ )
	{
		FMOD_Channel_Stop( (*i)->channel );
	}
}

void Audio::StopAllExcept( std::string name )
{
	for( Iterator i = samples.begin(); i != samples.end(); i++ )
	{
		if( (*i)->getName() != name )
		{
			FMOD_Channel_Stop( (*i)->channel );
		}
	}
}











